package gui;

import java.sql.SQLException;

import com.trolltech.qt.gui.*;

import gestionBDD.accesBDD;

public class fenetreBDD extends QWidget {
	
    private QLineEdit URL ;
    private QLineEdit Login ;
    private QLineEdit Password ;

    /**
     * Constructeur de la fenêtre Connexion MySQL
     */
	public fenetreBDD() {
				
        /** Login's GroupBox' **/
        QGroupBox albumGroup = new QGroupBox(tr("Connexion à la BDD"));
 
        /* Login Line Edit with Label */
        QLabel urlLabel = new QLabel(tr("URL :"));
        URL = new QLineEdit("jdbc:mysql://localhost/photo_num");
        URL.setFocus();
        
        /* Password LineEdit with Label */
        QLabel loginLabel = new QLabel(tr("Login :"));
        Login = new QLineEdit("root");

        /* Password LineEdit with Label */
        QLabel passwordLabel = new QLabel(tr("Password :"));
        Password = new QLineEdit("password");      
      
        
        /* Login's Layout */
        QGridLayout albumLayout = new QGridLayout();
        albumLayout.addWidget(urlLabel, 0, 0);
        albumLayout.addWidget(URL, 0, 1);
        albumLayout.addWidget(loginLabel, 1, 0);
        albumLayout.addWidget(Login, 1, 1);
        albumLayout.addWidget(passwordLabel, 2, 0);
        albumLayout.addWidget(Password, 2, 1);
        albumGroup.setLayout(albumLayout);

        /* Dialog Button Box */
        QDialogButtonBox boutonOk = new QDialogButtonBox();
        boutonOk.addButton(QDialogButtonBox.StandardButton.Ok);
        boutonOk.clicked.connect(this, "connecter()");

        // Dialog Layout
        QGridLayout layout = new QGridLayout();
        layout.addWidget(albumGroup, 0, 0);
        layout.addWidget(boutonOk, 1, 0);
        setWindowTitle(tr("Connexion MySQL"));
        setLayout(layout);

	}

	/**
	 * Initialise la connexion à la validation du formulaire
	 * @throws SQLException
	 */
	public void connecter() throws SQLException {
		try {
			accesBDD.initConnection2(URL.text(), Login.text(), Password.text());
			this.hide() ;
		}
		catch(SQLException e) {
			System.out.println(e);
		}
	}
}
